// pages/one/index.js
// 异步操作要复制this，同步操作不需要
let App = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		img_prefix: '',
		word: '',
		isQuery: false,
		img: '',
		spelling: '',
		paraphrase: '',
		calligrapher_name: '',
		calligrapher_type: 0,
		source_type: '历代'
	},

	wordInput(e) {
		this.setData({word: e.detail.value});
	},

	clear() {
		this.setData({word: ''});
	},

	submit() {
		// 有请求发送涉及到异步操作
		let _this = this;
		
		if (require("../../utils/util").verifyNotEmpty(_this.data.word) && require("../../utils/util").verifyChinese(_this.data.word) ) {
			let openId = wx.getStorageSync('openId');
			if (!openId) {
				wx.showModal({
					content: '请先进行登录',
					success (res) {
						if (res.confirm) {
							wx.navigateTo({
								url: '../login/login',
							})
						}else {
							return false;
						}
					}
				})
			}else {
				App._get('dic/single/word/', {code: openId, word: _this.data.word}, result => {
					result = JSON.parse(result);
					result['isQuery'] = true;
					if (result['calligrapher_type'] === 0) {
						result['img_prefix'] = App.api_root + 'storage/dictionary/single/ancient';
					} else if (result['calligrapher_type'] === 1) {
						result['img_prefix'] = App.api_root + 'storage/dictionary/single/modern';
						result['source_type'] = '现代';
					} else {
						result['img_prefix'] = App.api_root + 'storage/dictionary/single/user';
						result['source_type'] = '墨友';
					}
					_this.setData(result);
					console.log(this.data);
				})
			}
		} else {  // 就一个字直接清空
			_this.clear()
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},


	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})